﻿using Notepad.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace Notepad
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Hide();
            InitNotifyIcon();
            GetNode();
        }

        private void GetNode()
        { 
            DirectoryInfo folder = new DirectoryInfo(CommonHelper.SavePath);        
            if(!folder.Exists)
            {
                folder.Create();
            }
            if (folder.GetFiles().Length == 0)
            {
                var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                new XmlHelper().CreateXml(fileName);
                Note nod = new Note(new Model.Nodes());
                nod.Tag = fileName;
                nod.Show();
            }
            else
            {
                foreach (var file in folder.GetFiles())
                {
                    var md = new XmlHelper().GetXmlContent(file.FullName);
                    Note nod = new Note(md);
                    nod.Tag = System.IO.Path.GetFileNameWithoutExtension(file.Name);
                    nod.Show();
                }
            }
            
        }

        private void InitNotifyIcon()
        {
            var note = new NotifyIcon();
            note.Text = "黑脸撅便签纸";
            note.Visible = true;  
            note.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            System.Windows.Forms.MenuItem addMenu = new System.Windows.Forms.MenuItem("新建");
            System.Windows.Forms.MenuItem closeMenu = new System.Windows.Forms.MenuItem("退出");
            addMenu.Click += new EventHandler(AddMenu);
            closeMenu.Click += new EventHandler(CloseMenu); 
            System.Windows.Forms.MenuItem[] childMenu = new System.Windows.Forms.MenuItem[] { addMenu, closeMenu };
            note.ContextMenu = new System.Windows.Forms.ContextMenu(childMenu);
            note.ShowBalloonTip(1000, "提示", "欢迎使用-黑脸撅便签纸", ToolTipIcon.Info);
        }

        private void AddMenu(object sender, EventArgs e)
        {
            var newFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            new XmlHelper().CreateXml(newFileName);
            Note newNote = new Note(new Model.Nodes());
            newNote.Tag = newFileName;
            newNote.Show();
        }

        private void CloseMenu(object sender, EventArgs e)
        { 
            var process = System.Diagnostics.Process.GetProcessesByName("Notepad");
            foreach (System.Diagnostics.Process pro in process)
            {
                if (pro.MainModule.ModuleName.Contains("Notepad"))
                {
                    pro.Kill();
                }
            }
        }

    }
}
